//
//  EditTableViewController.m
//  11210
//
//  Created by student on 16/3/7.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "EditTableViewController.h"

#import "BIDPresident.h"

#import "PresidentDetailTableViewController.h"

@interface EditTableViewController ()<PresidentDetailTableViewControllerDelegate>

@property(nonatomic,strong)NSMutableArray *list;

@property(nonatomic,strong)NSIndexPath *curSel;

@end

@implementation EditTableViewController

-(NSMutableArray *)list{
    if (_list==nil) {
        _list=[NSMutableArray array];
        NSString *path=[[NSBundle mainBundle] pathForResource:@"Presidents" ofType:@"plist"];
        NSData *data=[NSData dataWithContentsOfFile:path];
        NSKeyedUnarchiver *unArch=[[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        NSArray *arr=[unArch decodeObjectForKey:@"Presidents"];
        [_list addObjectsFromArray:arr];
        
    }
    return _list;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}



#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    BIDPresident *pre=self.list[indexPath.row];
    cell.textLabel.text=pre.name;
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@-%@",pre.fromYear,pre.toYear];
 
    
    return cell;
}

//当沿着连线跳转的时候触发的方法
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    PresidentDetailTableViewController *nextVC=segue.destinationViewController;
    nextVC.delegate=self;
    nextVC.pre=self.list[self.curSel.row];
}

// 将要选中
// 注意！ 在此方法里记录你选中的行
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s",__func__);
    self.curSel = indexPath;// 记录当前选中的
    return indexPath;
}

// 已经选中
// 此方法在prepareForsegue之后运行的
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s",__func__);
}


#pragma mark PresidentDetailVC Delegate Method
- (void)refresh
{
    [self.tableView reloadData];
}


@end
